package org.ediba.siri.web.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.publication.domain.PublicationLevel;
import org.ediba.siri.core.publication.service.PublicationService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class PublicationLevelController extends Controller {
	
	private PublicationService publicationService;
	public void setPublicationService(PublicationService publicationService) {this.publicationService = publicationService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, Command command){
		
		String name = null;
		try {name = command.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		PublicationLevel publicationLevel = this.publicationService.levelCreate(name);
				
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", publicationLevel);
		_result.put("total", 1);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, Command command){
		
		Integer id = null;
		String name = null;
		try {
			id = command.getData().getInt("id");
			name = command.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		PublicationLevel publicationLevel = this.publicationService.levelRetrieve(id);
		publicationLevel.setName(name);
		publicationLevel = this.publicationService.levelUpdate(publicationLevel);
					
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", publicationLevel);
		_result.put("total", 1);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, Command command){
		
		List<PublicationLevel> publicationLevels = this.publicationService.levelRetrieve();
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", publicationLevels);
		_result.put("total", publicationLevels.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, Command command){
				
		Integer id = null;
		try {id = command.getData().getInt("id");
		} catch (JSONException e) {e.printStackTrace();}
		
		boolean success;
		String message = "Error al eliminar nivel";
		PublicationLevel publicationLevel = this.publicationService.levelRetrieve(id);
		if (publicationLevel.getPublications().isEmpty())
			success = this.publicationService.levelDelete(id);
		else {
			success = false;
			message = "El nivel que intenta eliminar tiene publicaciones asociadas";
		}
					
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", success);
		_result.put("message", message);
		return _result;
	}	
}
